package com.ruoyi.business.jizule.controller;

import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.PageInfo;
import com.ruoyi.business.jizule.domain.Jc41;
import com.ruoyi.business.jizule.mapper.Jc41Mapper;
import com.ruoyi.business.jizule.service.Jc41Service;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


//响应前端请求
@RestController
@RequestMapping("/business/jizule/jc41")
public class Jc41Controller {
    @Autowired
    private Jc41Mapper jc41Mapper;
    @Autowired
    private Jc41Service jc41Service;

    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Jc41 jc41)
    {
        PageUtils.startPage();
//        搜索记录时用的
        List<Jc41> jc41List = jc41Mapper.selectReportList(jc41);
        PageUtils.clearPage();
        return getDataTable(jc41List);
    }
    protected TableDataInfo getDataTable(List<?> list)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

    @GetMapping(value = "/get/{id}")

    /*根据id输出jc41整条记录*/
    public AjaxResult get(@PathVariable Long id){
        Jc41 jc41 = jc41Mapper.selectByPrimaryKey(id);
        System.out.println(JSON.toJSONString(jc41));
        if (null == jc41) {
            return AjaxResult.error("没有记录");
        }
        return AjaxResult.success(jc41);
    }

    @PostMapping("/insert")
    public AjaxResult insert(@Valid @RequestBody Jc41 jc41, BindingResult result) {
        jc41Service.insert(jc41);
        return AjaxResult.success("成功插入！");
/*        try{
            if (result.hasErrors()) {
                List<FieldError> errors = result.getFieldErrors();
                Map<String, String> map = new HashMap<>();
                for(FieldError err : errors) {
                    String beforeErrorInfo = map.get(err.getField()) == null ? "":map.get(err.getField())+",";
                    map.put(err.getField(), beforeErrorInfo + err.getDefaultMessage());
                }
                return AjaxResult.error("表单校验失败！",map);
            }
            if (jc41.getItems() == null || jc41.getItems().size() == 0) {
                ObjectError error = new ObjectError("items","表格数据为空！");
                return AjaxResult.error("表单校验失败！",error);
            }

            jc41Service.insert(jc41);
            return AjaxResult.success("成功插入！");
        }catch (Exception e){
            return AjaxResult.error("插入失败："+e.getMessage()+"!");
        }*/
    }

    @PostMapping("/update")
    public AjaxResult update(@Valid @RequestBody Jc41 jc41, BindingResult result) {
        jc41Service.update(jc41);
        return AjaxResult.success("成功更新！");
        /*try{
            if (result.hasErrors()) {
                List<FieldError> errors = result.getFieldErrors();
                Map<String, String> map = new HashMap<>();
                for(FieldError err : errors) {
                    String beforeErrorInfo = map.get(err.getField()) == null ? "":map.get(err.getField())+",";
                    map.put(err.getField(), beforeErrorInfo + err.getDefaultMessage());
                }
                return AjaxResult.error("表单校验失败！",map);
            }
            if (jc41.getItems() == null || jc41.getItems().size() == 0) {
                ObjectError error = new ObjectError("items","表格数据为空！");
                return AjaxResult.error("表单校验失败！",error);
            }

            jc41Service.update(jc41);
            return AjaxResult.success("成功更新！");
        }catch (Exception e){
            return AjaxResult.error("更新失败："+e.getMessage()+"!");
        }*/
    }

    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Long id){
        jc41Service.delete(id);
        return AjaxResult.success("成功删除！");
/*        try{
            jc41Service.delete(id);
            return AjaxResult.success("成功删除！");
        }catch (Exception e){
            return AjaxResult.error("删除失败："+e.getMessage()+"!");
        }*/
    }
}
